package com.zryc.shop.ums.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.ums.entity.Resource;
import com.zryc.shop.ums.entity.ResourceCategory;
import com.zryc.shop.ums.mapper.ResourceMapper;
import com.zryc.shop.ums.service.IResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 后台资源表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-16
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements IResourceService {

    /**
     * 分页查询资源
     *
     * @param pageNum
     * @param pageSize
     * @param nameKeyword
     * @param urlKeyword
     * @param categoryId
     * @return
     */
    @Override
    public Page<Resource> list(Integer pageNum, Integer pageSize, String nameKeyword, String urlKeyword, Integer categoryId) {
        Page<Resource> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(nameKeyword)) {
            wrapper.like("name", nameKeyword);
        }

        if (urlKeyword != null) {
            wrapper.eq("url", urlKeyword);
        }

        if (categoryId != null) {
            wrapper.eq("category_id", categoryId);
        }
        return page(page, wrapper);
    }

    /**
     * 更新资源信息
     *
     * @param id
     * @param resource
     * @return
     */
    @Override
    public Object updateResource(Long id, Resource resource) {
        UpdateWrapper<Resource> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        baseMapper.update(resource,updateWrapper);
        return null;
    }

    /**
     * 根据角色id查询资源列表
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Resource> getResourceByRoleId(Long roleId) {
        return baseMapper.getResourceByRoleId(roleId);
    }

    /**
     * 删除资源以及中间表记录
     *
     * @param id
     * @return
     */
    @Override
    public Object deleteResourceById(Long id) {
        baseMapper.deleteById(id);
        baseMapper.deleteForMiddle(id);
        return "删除成功";
    }
}
